package com.meepo.framework.design.userinfo

import android.content.Context
import android.text.TextUtils
import android.util.AttributeSet
import android.view.View
import android.widget.LinearLayout
import com.meepo.framework.R
import com.meepo.framework.design.helper.ImageLoader
import kotlinx.android.synthetic.main.userinfo_layout.view.*

class UserInfoView @JvmOverloads constructor(private val mContext: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0, defStyleRes: Int = 0) : LinearLayout(mContext, attrs, defStyleAttr, defStyleRes) {

    private var mUserInfo: UserInfo? = null

    fun setUserInfo(userInfo: UserInfo?) {
        mUserInfo = userInfo
        notifyDataChanged()
    }

    private fun notifyDataChanged() {

        mUserInfo?.let {

            if (!TextUtils.isEmpty(it.avatar)) {
                ImageLoader.loadImage(mContext, it.avatar, avatar_ci)
            } else {
                avatar_ci.visibility = View.GONE
            }

            if (!TextUtils.isEmpty(it.username)) {
                username_tv.text = it.username
            } else {
                username_tv.visibility = View.GONE
            }

            if (!TextUtils.isEmpty(it.tag)) {
                user_tagview.text = it.tag
            } else {
                user_tagview.visibility = View.GONE
            }

            if (!TextUtils.isEmpty(it.desc)) {
                user_desc.text = it.desc
            } else {
                user_desc.visibility = View.GONE
            }
        }
    }

    init {
        val userInfoView = View.inflate(mContext, R.layout.userinfo_layout, null)
        this.addView(userInfoView)
    }
}